﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2013-12-24 14:30
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BassDrop.Core.Common;
using BassDrop.Core.Common.Cloud;
using BassDrop.Core.Common.Localization;
using BassDrop.Core.Common.Plugins;
using BassDrop.DesktopClient.Utilities;

namespace BassDrop.DesktopClient.UI.Controls
{
	/// <summary>
	/// Shows information about and provides interaction to a cloud provider or an instance of one
	/// </summary>
	public partial class CloudUI : UserControl
	{
		private BassDropPluginBase m_cloudProvider;
		private CloudConnectionBase m_cloudInstance;

		/// <summary>
		/// Do not use
		/// </summary>
		private CloudUI()
		{
			InitializeComponent();

			Strings.StaticPropertyChanged += Strings_StaticPropertyChanged;
		}

		/// <summary>
		/// Creates a new UI for a cloud provider
		/// </summary>
		/// <param name="cloudProvider"></param>
		public CloudUI( BassDropPluginBase cloudProvider )
			: this()
		{
			m_cloudProvider = cloudProvider;
			m_cloudProvider.PropertyChanged += plugin_PropertyChanged;

			DoLayout();
		}

		/// <summary>
		/// Creates a new UI for a cloud provider instance
		/// </summary>
		/// <param name="cloudInstance"></param>
		public CloudUI( CloudConnectionBase cloudInstance )
			: this()
		{
			m_cloudInstance = cloudInstance;
			m_cloudInstance.PluginDescriptor.PropertyChanged += plugin_PropertyChanged;

			DoLayout();
		}

		private void DoLayout()
		{
			Stream iconStream = null;

			if ( m_cloudInstance != null )
			{
				string toolTipText = m_cloudInstance.PluginDescriptor.LongDescription;
				ToolTip = toolTipText; // TODO: szebben, ne egyesével

				LabelName.Content = m_cloudInstance.InstanceName; // TODO: legyen szerkeszthető
				LabelName.ToolTip = toolTipText;
				iconStream = m_cloudInstance.PluginDescriptor.Icon;
				ImageCloudProvider.ToolTip = toolTipText;
				ButtonAction.Content = Strings.LabelCloudUIButtonActionManage;
			}
			else if ( m_cloudProvider != null )
			{
				string toolTipText = m_cloudProvider.LongDescription;
				ToolTip = toolTipText;

				LabelName.Content = m_cloudProvider.Name;
				LabelName.ToolTip = toolTipText;
				iconStream = m_cloudProvider.Icon;
				ImageCloudProvider.ToolTip = toolTipText;
				ButtonAction.Content = Strings.LabelCloudUIButtonActionCreate;
			}

			try
			{
				BitmapImage icon = new BitmapImage();
				icon.BeginInit();
				icon.StreamSource = iconStream;
				icon.CacheOption = BitmapCacheOption.OnLoad;
				icon.EndInit();
				icon.Freeze();
				ImageCloudProvider.Source = icon;
			}
			catch ( Exception )
			{
				// Icon not found, ignore
			}
		}

		private void Strings_StaticPropertyChanged( object sender, System.ComponentModel.PropertyChangedEventArgs e )
		{
			DoLayout();
		}

		private void plugin_PropertyChanged( object sender, System.ComponentModel.PropertyChangedEventArgs e )
		{
			DoLayout();
		}

		private async void ButtonAction_Click( object sender, RoutedEventArgs e )
		{
			Exception error = null;

			try
			{
				if ( m_cloudInstance != null )
				{
					// Manage instance
					((MainWindow) App.Instance.MainWindow).ShowSettingsFor( m_cloudInstance );
				}
				else if ( m_cloudProvider != null )
				{
					// Create new instance	
					var instance = await m_cloudProvider.GetOrCreateInstance( m_cloudProvider.Name + " " + (m_cloudProvider.GetInstances().Count() + 1) );

					// TODO: ezt átvinni Core.Common-ba
					if ( Controller.Instance.PluginManager.GetDefaultCloudProvider() == null )
					{
						((CloudOptionsSetBase) instance.Options).IsDefault = true;
					}

					instance.Save( Controller.Instance.ApplicationSettings );
					((MainWindow) App.Instance.MainWindow).ShowSettingsFor( instance );
				}
			}
			catch ( Exception ex )
			{
				error = ex;
			}

			if ( error != null )
			{
				await error.Prompt( App.Instance.MainWindow );
			}
		}
	}
}
